package MEET.src.View;

import MEET.src.code.Meeting;
import MEET.src.code.UserDao;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Aduit extends JDialog {
    // 常量定义
    private static final int WINDOW_WIDTH = 1200;
    private static final int WINDOW_HEIGHT = 700;
    private static final int DIALOG_WIDTH = 650;
    private static final int DIALOG_HEIGHT = 600;
    // 校区对应的楼宇数组
    private static final String[] LINGANG_BUILDINGS = {
            "学术楼", "红楼", "经外楼", "1号教学楼", "二号教学楼", "三号教学楼"
    };

    private static final String[] YANGPU_BUILDINGS = {
            "杨浦会议厅", "杨浦报告厅"
    };

    // UI组件
    private JTable meetingTable;
    private DefaultTableModel tableModel;
    private JCheckBox chkHideExpired;
    private JButton btnUpdate, btnCancel, btnViewDetails;
    private JDialog detailDialog, confirmDialog, updateDialog;
    private JPanel contentPane;

    // 时间格式化工具
    private SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    // 更新对话框组件
    private JTextField updateMnameField, updateHostField, updateRoomField;
    private JComboBox<String> updatePlaceCombo, updateLocationCombo;
    private JComboBox<String> updateFormCombo, updateTypeCombo, updateStatusCombo;
    private JSpinner updateStartSpinner, updateEndSpinner;

    public Aduit() {
        initUI();
        loadMeetings();
        setupEventListeners();
    }

    private void initUI() {
        setTitle("上海电力大学会议预订系统 - 会议审核");
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); // 设置关闭操作为隐藏窗口
        setModalityType(Dialog.ModalityType.APPLICATION_MODAL); // 设置为应用模式对话框
        setLocationRelativeTo(null);

        // 主面板设置
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 5));
        setContentPane(contentPane);

        // 1. 顶部标题栏
        createHeaderPanel();

        // 2. 内容区
        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.setBorder(new EmptyBorder(15, 15, 15, 15));
        mainPanel.setBackground(new Color(240, 245, 249));

        // 内部标题
        JPanel innerTitlePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        innerTitlePanel.setBackground(new Color(240, 245, 249));
        JLabel innerTitle = new JLabel("会议审核管理");
        innerTitle.setFont(new Font("微软雅黑", Font.BOLD, 18));
        innerTitle.setForeground(new Color(0, 125, 153));
        innerTitlePanel.add(innerTitle);
        mainPanel.add(innerTitlePanel, BorderLayout.NORTH);

        // 筛选选项面板
        JPanel toolPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
        toolPanel.setBorder(BorderFactory.createTitledBorder("筛选选项"));
        toolPanel.setBackground(Color.WHITE);
        chkHideExpired = new JCheckBox("隐藏已过期记录", true);
        chkHideExpired.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        toolPanel.add(chkHideExpired);
        mainPanel.add(toolPanel, BorderLayout.NORTH);

        // 会议表格
        tableModel = new DefaultTableModel(new Object[]{"会议ID", "预定者", "会议室", "会议名称",
                "开始时间", "结束时间", "状态"}, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        meetingTable = new JTable(tableModel) {
            @Override
            public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                Component c = super.prepareRenderer(renderer, row, column);

                // 交替行背景色
                if (row % 2 == 0) {
                    c.setBackground(new Color(240, 248, 255)); // 淡蓝色
                } else {
                    c.setBackground(Color.WHITE);
                }

                // 状态列特殊样式
                if (column == 6) {
                    String status = (String) getValueAt(row, column);
                    JLabel label = new JLabel(status);
                    label.setOpaque(true);
                    label.setHorizontalAlignment(SwingConstants.CENTER);

                    switch (status) {
                        case "未通过":
                            label.setBackground(new Color(255, 200, 200));
                            label.setForeground(Color.RED);
                            break;
                        case "已通过":
                            label.setBackground(new Color(200, 255, 200));
                            label.setForeground(new Color(0, 150, 0));
                            break;
                        case "待审核":
                            label.setBackground(new Color(255, 255, 200));
                            label.setForeground(Color.DARK_GRAY);
                            break;
                    }
                    return label;
                }

                return c;
            }
        };

        // 表格样式设置
        meetingTable.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        meetingTable.setRowHeight(35);
        meetingTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        meetingTable.setIntercellSpacing(new Dimension(0, 1));
        meetingTable.setShowGrid(false);

        // 表头样式
        JTableHeader header = meetingTable.getTableHeader();
        header.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        header.setBackground(new Color(70, 130, 180));
        header.setPreferredSize(new Dimension(header.getWidth(), 40));

        // 列宽调整
        TableColumnModel columnModel = meetingTable.getColumnModel();
        columnModel.getColumn(0).setPreferredWidth(80);  // ID
        columnModel.getColumn(1).setPreferredWidth(100); // 预定者
        columnModel.getColumn(2).setPreferredWidth(150); // 会议室
        columnModel.getColumn(3).setPreferredWidth(200); // 会议名称
        columnModel.getColumn(4).setPreferredWidth(180); // 开始时间
        columnModel.getColumn(5).setPreferredWidth(180); // 结束时间
        columnModel.getColumn(6).setPreferredWidth(100); // 状态

        JScrollPane tableScroll = new JScrollPane(meetingTable);
        tableScroll.setBorder(BorderFactory.createTitledBorder("会议列表"));
        tableScroll.setBackground(Color.WHITE);
        mainPanel.add(tableScroll, BorderLayout.CENTER);

        // 底部操作按钮
        JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 15, 10));
        btnPanel.setBorder(new EmptyBorder(5, 0, 5, 0));
        btnPanel.setBackground(new Color(240, 245, 249));

        btnViewDetails = createStyledButton("查看详情", new Color(70, 130, 180));
        btnUpdate = createStyledButton("更新信息", new Color(46, 139, 87));
        btnCancel = createStyledButton("取消会议", new Color(220, 20, 60));

        btnPanel.add(btnViewDetails);
        btnPanel.add(btnUpdate);
        btnPanel.add(btnCancel);
        mainPanel.add(btnPanel, BorderLayout.SOUTH);

        contentPane.add(mainPanel, BorderLayout.CENTER);

        // 初始化对话框
        initDetailDialog();
        initConfirmDialog();
        initUpdateDialog();
    }

    private void createHeaderPanel() {
        JPanel headerPanel = new JPanel();
        headerPanel.setBackground(new Color(0, 125, 153));
        headerPanel.setPreferredSize(new Dimension(100, 50));

        JLabel lblTitle = new JLabel("上海电力大学会议预订系统");
        lblTitle.setForeground(Color.WHITE);
        lblTitle.setFont(new Font("微软雅黑", Font.BOLD, 20));

        headerPanel.add(lblTitle);
        contentPane.add(headerPanel, BorderLayout.NORTH);
    }

    private JButton createStyledButton(String text, Color bgColor) {
        JButton button = new JButton(text);
        button.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        button.setBackground(bgColor);
        button.setForeground(Color.BLACK);
        button.setFocusPainted(false);
        button.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(bgColor.darker(), 1),
                BorderFactory.createEmptyBorder(8, 20, 8, 20)
        ));
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));

        // 添加悬停效果
        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                button.setBackground(bgColor.brighter());
            }

            @Override
            public void mouseExited(MouseEvent e) {
                button.setBackground(bgColor);
            }
        });

        return button;
    }

    private void loadMeetings() {
        List<Meeting> meetings = UserDao.loadMeetings(chkHideExpired.isSelected());
        tableModel.setRowCount(0);

        for (Meeting meeting : meetings) {
            tableModel.addRow(new Object[]{
                    meeting.getMID(),
                    meeting.getMname(),
                    meeting.getLocation() + "-" + meeting.getPlace() + "-" + meeting.getRoom(),
                    meeting.getName(),
                    dateTimeFormat.format(meeting.getMstarttime()),
                    dateTimeFormat.format(meeting.getMendtime()),
                    meeting.getStatus()
            });
        }
    }

    private void initDetailDialog() {
        detailDialog = new JDialog(this, "会议详情", true);
        detailDialog.setSize(450, 350);
        detailDialog.setLayout(new BorderLayout());
        detailDialog.setLocationRelativeTo(this);
    }

    private void initConfirmDialog() {
        confirmDialog = new JDialog(this, "确认操作", true);
        confirmDialog.setSize(350, 180);
        confirmDialog.setLayout(new BorderLayout());
        confirmDialog.setLocationRelativeTo(this);

        JPanel content = new JPanel(new BorderLayout(10, 10));
        content.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        JLabel label = new JLabel("确定要取消该会议吗？");
        label.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        label.setHorizontalAlignment(SwingConstants.CENTER);
        content.add(label, BorderLayout.CENTER);

        JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
        JButton btnConfirm = createStyledButton("确认取消", new Color(220, 20, 60));
        JButton btnCancel = createStyledButton("返回", new Color(120, 120, 120));

        btnConfirm.addActionListener(e -> {
            int selectedRow = meetingTable.getSelectedRow();
            if (selectedRow != -1) {
                String mid = (String) tableModel.getValueAt(selectedRow, 0);
                UserDao.cancelMeeting(mid);
                tableModel.removeRow(selectedRow);
            }
            confirmDialog.dispose();
        });

        btnCancel.addActionListener(e -> confirmDialog.dispose());

        btnPanel.add(btnConfirm);
        btnPanel.add(btnCancel);
        content.add(btnPanel, BorderLayout.SOUTH);
        confirmDialog.add(content);
    }

    private void initUpdateDialog() {
        updateDialog = new JDialog(this, "更新会议信息", true);
        updateDialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
        updateDialog.setLayout(new BorderLayout());
        updateDialog.setLocationRelativeTo(this);

        // 主面板
        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.setBorder(new EmptyBorder(15, 15, 15, 15));
        mainPanel.setBackground(new Color(240, 245, 249));

        // 内部标题
        JPanel innerTitlePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        innerTitlePanel.setBackground(new Color(240, 245, 249));
        JLabel innerTitle = new JLabel("更新会议信息");
        innerTitle.setFont(new Font("微软雅黑", Font.BOLD, 18));
        innerTitle.setForeground(new Color(0, 125, 153));
        innerTitlePanel.add(innerTitle);
        mainPanel.add(innerTitlePanel, BorderLayout.NORTH);

        // 表单面板
        JPanel formPanel = new JPanel(new GridBagLayout());
        formPanel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(200, 200, 200)),
                new EmptyBorder(15, 15, 15, 15)
        ));
        formPanel.setBackground(Color.WHITE);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(8, 8, 8, 8);
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // 使用统一字体
        Font labelFont = new Font("微软雅黑", Font.BOLD, 14);
        Font fieldFont = new Font("微软雅黑", Font.PLAIN, 14);

        // 会议主题
        gbc.gridx = 0; gbc.gridy = 0;
        JLabel lblMname = new JLabel("会议主题:");
        lblMname.setFont(labelFont);
        formPanel.add(lblMname, gbc);

        gbc.gridx = 1; gbc.gridwidth = 2; gbc.weightx = 1.0;
        updateMnameField = new JTextField();
        updateMnameField.setFont(fieldFont);
        formPanel.add(updateMnameField, gbc);

        // 预定者
        gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 1;
        JLabel lblHost = new JLabel("预定者:");
        lblHost.setFont(labelFont);
        formPanel.add(lblHost, gbc);

        gbc.gridx = 1; gbc.gridwidth = 2;
        updateHostField = new JTextField();
        updateHostField.setFont(fieldFont);
        formPanel.add(updateHostField, gbc);

        // 校区
        gbc.gridx = 0; gbc.gridy = 2;
        JLabel lblPlace = new JLabel("校区:");
        lblPlace.setFont(labelFont);
        formPanel.add(lblPlace, gbc);

        gbc.gridx = 1; gbc.gridwidth = 1;
        updatePlaceCombo = new JComboBox<>(new String[]{"临港", "杨浦"});
        updatePlaceCombo.setFont(fieldFont);
        updatePlaceCombo.addActionListener(new PlaceComboListener());
        formPanel.add(updatePlaceCombo, gbc);

        // 楼宇
        gbc.gridx = 2;
        JLabel lblLocation = new JLabel("楼宇:");
        lblLocation.setFont(labelFont);
        formPanel.add(lblLocation, gbc);

        gbc.gridx = 3; gbc.gridwidth = 1;
        updateLocationCombo = new JComboBox<>(LINGANG_BUILDINGS);
        updateLocationCombo.setFont(fieldFont);
        formPanel.add(updateLocationCombo, gbc);

        // 会议室
        gbc.gridx = 0; gbc.gridy = 3; gbc.gridwidth = 1;
        JLabel lblRoom = new JLabel("会议室:");
        lblRoom.setFont(labelFont);
        formPanel.add(lblRoom, gbc);

        gbc.gridx = 1; gbc.gridwidth = 3;
        updateRoomField = new JTextField();
        updateRoomField.setFont(fieldFont);
        formPanel.add(updateRoomField, gbc);

        // 会议时间
        gbc.gridx = 0; gbc.gridy = 4;
        JLabel lblTime = new JLabel("会议时间:");
        lblTime.setFont(labelFont);
        formPanel.add(lblTime, gbc);

        gbc.gridx = 1; gbc.gridwidth = 3;
        JPanel timePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));

        updateStartSpinner = new JSpinner(new SpinnerDateModel());
        JSpinner.DateEditor startEditor = new JSpinner.DateEditor(updateStartSpinner, "yyyy-MM-dd HH:mm");
        updateStartSpinner.setEditor(startEditor);
        updateStartSpinner.setFont(fieldFont);
        updateStartSpinner.setPreferredSize(new Dimension(180, 30));

        JLabel toLabel = new JLabel("至");
        toLabel.setFont(fieldFont);

        updateEndSpinner = new JSpinner(new SpinnerDateModel());
        JSpinner.DateEditor endEditor = new JSpinner.DateEditor(updateEndSpinner, "yyyy-MM-dd HH:mm");
        updateEndSpinner.setEditor(endEditor);
        updateEndSpinner.setFont(fieldFont);
        updateEndSpinner.setPreferredSize(new Dimension(180, 30));

        timePanel.add(updateStartSpinner);
        timePanel.add(toLabel);
        timePanel.add(updateEndSpinner);
        formPanel.add(timePanel, gbc);

        // 会议形式
        gbc.gridx = 0; gbc.gridy = 5; gbc.gridwidth = 1;
        JLabel lblForm = new JLabel("会议形式:");
        lblForm.setFont(labelFont);
        formPanel.add(lblForm, gbc);

        gbc.gridx = 1; gbc.gridwidth = 3;
        updateFormCombo = new JComboBox<>(new String[]{"线上会议", "线下会议"});
        updateFormCombo.setFont(fieldFont);
        formPanel.add(updateFormCombo, gbc);

        // 会议类型
        gbc.gridx = 0; gbc.gridy = 6; gbc.gridwidth = 1;
        JLabel lblType = new JLabel("会议类型:");
        lblType.setFont(labelFont);
        formPanel.add(lblType, gbc);

        gbc.gridx = 1; gbc.gridwidth = 3;
        updateTypeCombo = new JComboBox<>(new String[]{
                "学术会议", "讲座", "教学会议", "学生会议", "党建会议", "培训会议"
        });
        updateTypeCombo.setFont(fieldFont);
        formPanel.add(updateTypeCombo, gbc);

        // 状态
        gbc.gridx = 0; gbc.gridy = 7; gbc.gridwidth = 1;
        JLabel lblStatus = new JLabel("状态:");
        lblStatus.setFont(labelFont);
        formPanel.add(lblStatus, gbc);

        gbc.gridx = 1; gbc.gridwidth = 3;
        updateStatusCombo = new JComboBox<>(new String[]{"待审核", "已通过", "未通过"});
        updateStatusCombo.setFont(fieldFont);
        formPanel.add(updateStatusCombo, gbc);

        mainPanel.add(formPanel, BorderLayout.CENTER);

        // 按钮面板
        JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 15));
        JButton btnSubmit = createStyledButton("提交更新", new Color(70, 130, 180));
        JButton btnCancelBtn = createStyledButton("取消", new Color(120, 120, 120));

        btnSubmit.addActionListener(e -> {
            if (updateMeetingData()) {
                updateDialog.dispose();
                loadMeetings();
            }
        });

        btnCancelBtn.addActionListener(e -> updateDialog.dispose());

        btnPanel.add(btnSubmit);
        btnPanel.add(btnCancelBtn);
        mainPanel.add(btnPanel, BorderLayout.SOUTH);

        updateDialog.add(mainPanel);
    }

    // 校区选择监听器
    private class PlaceComboListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String selectedPlace = (String) updatePlaceCombo.getSelectedItem();
            updateLocationCombo.removeAllItems();

            if ("临港".equals(selectedPlace)) {
                for (String building : LINGANG_BUILDINGS) {
                    updateLocationCombo.addItem(building);
                }
            } else if ("杨浦".equals(selectedPlace)) {
                for (String building : YANGPU_BUILDINGS) {
                    updateLocationCombo.addItem(building);
                }
            }
        }
    }

    private void showMeetingDetails(String mid) {
        Meeting meeting = UserDao.getMeetingDetails(mid);
        if (meeting == null) return;

        JPanel content = new JPanel(new GridLayout(0, 2, 10, 10));
        content.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(200, 200, 200)),
                new EmptyBorder(20, 20, 20, 20)
        ));
        content.setBackground(Color.WHITE);

        // 使用统一字体
        Font labelFont = new Font("微软雅黑", Font.BOLD, 14);
        Font valueFont = new Font("微软雅黑", Font.PLAIN, 14);

        // 使用JLabel包装所有值
        addDetailRow(content, "会议ID:", new JLabel(meeting.getMID()), labelFont, valueFont);
        addDetailRow(content, "预定者:", new JLabel(meeting.getName()), labelFont, valueFont);
        addDetailRow(content, "会议主题:", new JLabel(meeting.getMname()), labelFont, valueFont);
        addDetailRow(content, "会议室:", new JLabel(meeting.getPlace() + "-" + meeting.getLocation() + "-" + meeting.getRoom()), labelFont, valueFont);
        addDetailRow(content, "会议时间:", new JLabel(
                dateTimeFormat.format(meeting.getMstarttime()) + " - " +
                        dateTimeFormat.format(meeting.getMendtime())
        ), labelFont, valueFont);
        addDetailRow(content, "会议类型:", new JLabel(meeting.getType()), labelFont, valueFont);
        addDetailRow(content, "会议形式:", new JLabel(meeting.getForm()), labelFont, valueFont);
        addDetailRow(content, "当前状态:", getStatusLabel(meeting.getStatus()), labelFont, valueFont);

        detailDialog.setContentPane(content);
        detailDialog.pack();
        detailDialog.setLocationRelativeTo(this);
        detailDialog.setVisible(true);
    }

    private void addDetailRow(JPanel panel, String label, Component valueComponent,
                              Font labelFont, Font valueFont) {
        JLabel lbl = new JLabel(label);
        lbl.setFont(labelFont);
        panel.add(lbl);

        // 设置值组件的字体
        if (valueComponent instanceof JLabel) {
            JLabel valueLabel = (JLabel) valueComponent;
            valueLabel.setFont(valueFont);
        }
        panel.add(valueComponent);
    }

    private JLabel getStatusLabel(String status) {
        JLabel label = new JLabel(status);
        label.setOpaque(true);
        label.setHorizontalAlignment(SwingConstants.CENTER);
        label.setBorder(BorderFactory.createEmptyBorder(3, 10, 3, 10));
        label.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        switch (status) {
            case "未通过":
                label.setBackground(new Color(255, 200, 200));
                label.setForeground(Color.RED);
                break;
            case "已通过":
                label.setBackground(new Color(200, 255, 200));
                label.setForeground(new Color(0, 150, 0));
                break;
            case "待审核":
                label.setBackground(new Color(255, 255, 200));
                label.setForeground(Color.DARK_GRAY);
                break;
        }
        return label;
    }

    private void setupEventListeners() {
        // 隐藏已过期记录
        chkHideExpired.addActionListener(e -> loadMeetings());

        // 查看详情
        btnViewDetails.addActionListener(e -> {
            int row = meetingTable.getSelectedRow();
            if (row != -1) {
                showMeetingDetails((String) tableModel.getValueAt(row, 0));
            } else {
                showMessage("请先选择一条会议记录");
            }
        });

        // 更新会议
        btnUpdate.addActionListener(e -> {
            int row = meetingTable.getSelectedRow();
            if (row == -1) {
                showMessage("请先选择要更新的会议记录");
                return;
            }

            Meeting meeting = UserDao.getMeetingDetails(
                    (String) tableModel.getValueAt(row, 0));

            if (meeting != null) {
                updateMnameField.setText(meeting.getMname());
                updateHostField.setText(meeting.getName());
                updatePlaceCombo.setSelectedItem(meeting.getLocation());
                updateRoomField.setText(meeting.getRoom());
                updateStartSpinner.setValue(meeting.getMstarttime());
                updateEndSpinner.setValue(meeting.getMendtime());
                updateFormCombo.setSelectedItem(meeting.getForm());
                updateTypeCombo.setSelectedItem(meeting.getType());
                updateStatusCombo.setSelectedItem(meeting.getStatus());

                // 设置楼宇
                updateLocationCombo.removeAllItems();
                String location = meeting.getLocation();
                if ("临港".equals(location)) {
                    for (String building : LINGANG_BUILDINGS) {
                        updateLocationCombo.addItem(building);
                    }
                } else if ("杨浦".equals(location)) {
                    for (String building : YANGPU_BUILDINGS) {
                        updateLocationCombo.addItem(building);
                    }
                }

                updateDialog.setVisible(true);
            }
        });

        // 取消会议
        btnCancel.addActionListener(e -> {
            if (meetingTable.getSelectedRow() != -1) {
                confirmDialog.setVisible(true);
            } else {
                showMessage("请先选择一条会议记录");
            }
        });
    }

    private boolean updateMeetingData() {
        int row = meetingTable.getSelectedRow();
        if (row == -1) {
            showMessage("请先选择要更新的会议记录");
            return false;
        }

        String mid = (String) tableModel.getValueAt(row, 0);
        Map<String, Object> params = new HashMap<>();

        params.put("Mname", updateMnameField.getText());
        params.put("Name", updateHostField.getText());
        params.put("Place", updatePlaceCombo.getSelectedItem());
        params.put("Location", updateLocationCombo.getSelectedItem());
        params.put("Room", updateRoomField.getText());

        // 修复时间转换问题
        java.util.Date startDate = (java.util.Date) updateStartSpinner.getValue();
        java.util.Date endDate = (java.util.Date) updateEndSpinner.getValue();

        params.put("Mstarttime", new Timestamp(startDate.getTime()));
        params.put("Mendtime", new Timestamp(endDate.getTime()));

        params.put("Form", updateFormCombo.getSelectedItem());
        params.put("Type", updateTypeCombo.getSelectedItem());
        params.put("Status", updateStatusCombo.getSelectedItem());

        return UserDao.updateMeeting(mid, params);
    }

    private void showMessage(String msg) {
        JOptionPane.showMessageDialog(this, msg, "系统提示",
                JOptionPane.INFORMATION_MESSAGE);
    }

    public static void main(String[] args) {
        UserDao.testConnection();
        SwingUtilities.invokeLater(() -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                new Aduit().setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
}